var api = require('../../utils/api.js')
var util = require('../../utils/util.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    mobile: '',
    province: '',
    city: '',
    county: '',
    front_img: '',
    back_img: '',
    shopName: '',
    shopMobile: '',
    region: ['请选择所在地区', '', '']
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    wx.hideShareMenu() // 隐藏分享按钮
    // console.log(options);
    //分享进入
    // 邀请人姓名
    if (options.shop_name) {
      this.setData({
        shopName: options.shop_name,
      });
    } else {
      this.setData({
        shopName: app.globalData.accountInfo.username
      });
    }

    //邀请人手机号码
    if (options.shop_mobile) {
      this.setData({
        shopMobile: options.shop_mobile,
      });
    } else {
      this.setData({
        shopMobile: app.globalData.accountInfo.mobile
      });
    }


  },

  // onShareAppMessage: function() {
  //   var shop_name = app.globalData.accountInfo.username;
  //   return {
  //     title: '邀请代理商',
  //     // path: '/pages/inviteAgentBeShared/inviteAgentBeShared?shop_name=' + shop_name,
  //     path: '/pages/inviteAgent/inviteAgent?shop_name=' + shop_name,
  //     imageUrl: ''
  //   }
  // },

  //上传图片
  editGoodsImgEvent: function(e) {
    var that = this;
    var imgtype = e.currentTarget.dataset.type
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed '], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        var tempFilePaths = res.tempFilePaths;
        util.showLoading("加载中...");
        for (var i = 0; i < tempFilePaths.length; i++) {
          wx.uploadFile({
            url: app.globalData.base_url + 'img/saveImage',
            filePath: tempFilePaths[i],
            name: 'file',
            success: function(res) {
              util.hiddenLoading();
              var data = JSON.parse(res.data);
              if (imgtype == 'front_img') {
                that.setData({
                  front_img: data.data.url
                })
              } else if (imgtype == 'back_img') {
                that.setData({
                  back_img: data.data.url
                })
              }
            },
            fail: function() {
              util.hiddenLoading();
            }
          })
        }
      }
    })
  },
  //输入名字
  inputName: function(e) {
    var that = this
    var name = e.detail.value;
    this.setData({
      name: name
    })
  },
  //输入手机号
  inputMobile: function(e) {
    var mobile = e.detail.value;
    this.setData({
      mobile: mobile
    })
  },
  // //输入省
  // inputProvince: function(e) {
  //   var province = e.detail.value;
  //   this.setData({
  //     province: province
  //   })
  // },
  // //输入市
  // inputCity: function(e) {
  //   var city = e.detail.value;
  //   this.setData({
  //     city: city
  //   })
  // },
  // //输入区
  // inputCounty: function(e) {
  //   var county = e.detail.value;
  //   this.setData({
  //     county: county
  //   })
  // },

  //选择省市区
  selectAddress: function (e) {
    var value = e.detail.value;
    this.setData({
      region: value
    })
  },

  //提交注册
  submit: function() {
    var that = this;
    if (this.data.name == '' || this.data.name == undefined) {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请输入姓名',
        confirmText: '知道了',
        success: function(res) {}
      })
      return false
    }

    if (!(/^1(3|4|5|7|8)\d{9}$/.test(this.data.mobile))) {
      // util.showError("请输入正确的手机号");
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请输入正确的手机号',
        confirmText: '知道了',
        success: function(res) {}
      })
      return false
    }

    if (this.data.region[1] == '' || this.data.region[1] == undefined) {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请选择所在地',
        confirmText: '知道了',
        success: function (res) { }
      })
      return false
    }

    if (this.data.front_img == '' || this.data.front_img == undefined) {
      // util.showError("请上传身份证正面");
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请上传身份证正面',
        confirmText: '知道了',
        success: function(res) {}
      })
      return false
    }

    if (this.data.back_img == '' || this.data.back_img == undefined) {
      // util.showError("请上传身份证反面");
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请上传身份证反面',
        confirmText: '知道了',
        success: function(res) {}
      })
      return false
    }

    var params = {
      name: that.data.name,
      mobile: that.data.mobile,
      id_front_img: that.data.front_img,
      id_back_img: that.data.back_img,
      province: that.data.region[0],
      city: that.data.region[1],
      county: that.data.region[2],
      shopname: that.data.shopName,
      shopmobile: that.data.shopMobile
    };

    api.POST2({
      params: params,
      url: app.globalData.base_url + "user/agencyReg",
      success: function(res) {
        if (res.data.code == '') {
          app.globalData.accountInfo.audit_status = 0
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: res.data.message,
            // content: '您的申请已经提交。结果将通过短信通知，请注意查收。',
            confirmText: '知道了',
            success: function(res) {
              wx.navigateToMiniProgram({
                appId: 'wx2f5360b509c07b8a',
                path: 'pages/index/index',
                envVersion: 'release', //develop	开发版 trial	体验版  release	正式版
              })
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: res.data.message,
            confirmText: '知道了',
            success: function(res) {
              wx.navigateToMiniProgram({
                appId: 'wx2f5360b509c07b8a',
                path: 'pages/index/index',
                envVersion: 'release', //develop	开发版 trial	体验版  release	正式版
              })
            }
          })
        }
      }
    })
  },

  previewImage: function(event) {
    // console.log(event.currentTarget.dataset.img);
    var current = event.currentTarget.dataset.img;
    console.log(current);

    wx.previewImage({
      current: '', //图片http链接
      urls: [], //图片http链接
    })
  }

})